(use-modules
 (gnu)
 (gnu system nss)
 (gnu services networking)
 (gnu services desktop)
 (gnu services base)
 (gnu services dbus)
 (gnu services security-token)
 (srfi srfi-1)
 (ice-9 textual-ports))

(use-package-modules
 bootloaders certs tex base emacs emacs-xyz networking wm xdisorg
 password-utils messaging syncthing gnupg security-token build-tools
 flex python package-management commencement man llvm maths texinfo
 bison ninja gnome gnome-xyz chromium admin ncurses browser-extensions
 tmux screen glib file-systems text-editors ssh networking pulseaudio
 compression curl image-viewers image gimp vim video music audio
 bittorrent kde-multimedia kde cups libreoffice abiword gnuzilla xorg
 linux fonts gtk qt freedesktop rsync fcitx5 embedded mc file
 version-control)

(include "recipes.scm")
(include "kernel.scm")

;; Here are the system packages list defined.

(define gs-dev-packages
  (list
   ;; build system packegs
   flex bison meson ninja emacs-meson-mode
   ;; compilers and interpreters
   python gcc-toolchain clang-toolchain stlink openocd
   (make-arm-none-eabi-nano-toolchain-7-2018-q2-update)
   (make-gdb-arm-none-eabi)
   ;; manuals and references
   mandoc texinfo devhelp))

(define gs-desktop-packages
  (list
   dbus gtk gtk+ qtbase-5 qtwayland-5 
   ;; sway and its related packages
   sway swaybg swaylock
   i3status rofi mako
   grim grimshot slurp
   pipewire wireplumber-minimal pavucontrol
   libappindicator libnotify
   ;; input method packages
   fcitx5 fcitx5-configtool
   fcitx5-rime
   fcitx5-gtk fcitx5-gtk4 fcitx5-qt
   ;; xdg packages
   xdg-desktop-portal-wlr
   xdg-desktop-portal-gtk
   xdg-desktop-portal
   xdg-user-dirs xdg-utils
   ;; themes and icons
   adwaita-icon-theme
   hicolor-icon-theme
   gnome-themes-extra
   ;; cool packages for X
   xrdb brightnessctl
   rxvt-unicode
   ;; font packages
   font-carlito
   font-openmoji
   font-gnu-unifont
   font-wqy-zenhei
   font-iosevka-term-slab
   ;; web browsing, chatting packages
   ungoogled-chromium/wayland
   icecat noscript/icecat
   ublock-origin/icecat
   keepassxc-browser/icecat
   evolution evolution-data-server
   transmission
   gajim gajim-openpgp gajim-omemo
   hexchat nheko caddy
   ;; viewer packages
   viewnior gimp shotwell
   vlc mpv rhythmbox playerctl
   okular
   ;; audio/video editors
   kid3 emacs-emms
   aegisub audacity mixxx
   ;; productivity packages
   cups epson-inkjet-printer-escpr
   simple-scan libreoffice abiword
   ;; secret management packages
   keepassxc syncthing))

(define gs-base-packages
  (append (list
   ;; system utils
   neofetch ncurses stow
   netcat openssh network-manager
   htop tmux screen units
   ;; file operation packages
   bzip2 zip unzip tar p7zip
   file mc bluez bluez-alsa
   bcachefs-tools btrfs-progs
   ;; security packages
   gnupg pinentry
   pcsc-lite pcsc-tools ccid
   yubico-piv-tool
   ;; version control packages
   mercurial git cvs
   ;; networking packages
   nss-certs proxychains-ng
   mtr net-tools curl 
   ;; editor packages
   xxd nano emacs emacs-company) %base-packages))

(define gs-all-packages
  (append gs-base-packages (append gs-dev-packages gs-desktop-packages)))

;; Yes, we have services to be installed as well.

(define gs-desktop-services
  (list
   (service dbus-root-service-type)
   (service elogind-service-type)
   (service polkit-service-type)
   (service upower-service-type)
   (service sane-service-type)
   (service bluetooth-service-type)))

(define gs-base-services
  (list
   (service network-manager-service-type)
   (service modem-manager-service-type)
   (service wpa-supplicant-service-type)
   (service pcscd-service-type)
   (service ntp-service-type)))

;; The security tokens also require custom udev rules.

(define %canokeys-udev-rules
  (udev-rule
   "69-canokeys.rules"
   (string-append
    "KERNEL==\"hidraw*\", SUBSYSTEM==\"hidraw\", ATTRS{idVendor}==\"20a0\", ATTRS{idProduct}==\"42d4\", GROUP=\"plugdev\", MODE=\"0660\"\n"
    "SUBSYSTEMS==\"usb\", ATTR{idVendor}==\"20a0\", ATTR{idProduct}==\"42d4\", GROUP=\"plugdev\", MODE=\"0660\"\n")))

(define gs-base-services
  (append
   (list (udev-rules-service 'canokeys %canokeys-udev-rules
			     #:groups '("plugdev")))
   gs-base-services))

;; Right, we can construct all the services now.

(define gs-mirrored-base-services
  (modify-services
   %base-services
   (guix-service-type
    configs => (guix-configuration
		(inherit configs)
		(substitute-urls
		 '("https://bordeaux.fsf.moe"
		   "https://mirror.sjtu.edu.cn/guix"))))))

(define gs-all-services
  (append gs-mirrored-base-services
	  (append gs-desktop-services gs-base-services)))

;; Now there is our operating system definations.

(operating-system
 (kernel-arguments (append (list "i915.enable_guc=0" "mem_sleep=deep")
			   %default-kernel-arguments))

 ;; Customized kernel & firmwares.
 ;; (kernel linux-nuc-x15)
 ;; (firmware
 ;;  (list
 ;;   ibt-firmware-nonfree
 ;;   iwlwifi-firmware-nonfree
 ;;   i915-firmware-nonfree))
 (kernel linux-libre-6.6)
 
 (host-name "intelnuc")
 (timezone "Asia/Shanghai")
 (locale "en_US.utf8")
 
 (bootloader (bootloader-configuration
              (bootloader grub-efi-bootloader)
              (targets '("/boot/efi"))))

 (file-systems (append
                (list (file-system
                       (device (file-system-label "rootfs"))
                       (mount-point "/")
                       (type "btrfs"))
                      (file-system
                       (device (uuid "D523-CAA8" 'fat))
                       (mount-point "/boot/efi")
                       (type "vfat"))
		      (file-system
		       (device (file-system-label "homefs"))
		       (mount-point "/home")
		       (type "btrfs")))
                %base-file-systems))

 (users (cons (user-account
               (name "goodspeed")
               (comment "William Goodspeed")
               (group "users")
               (supplementary-groups '("wheel" "netdev"
                                       "audio" "video"
				       "plugdev")))
              %base-user-accounts))

 (packages gs-all-packages)

; (services (append
;	    (list
;	     (service dhcp-client-service-type))
;	    %base-services))

 (services gs-all-services))
